package com.itheima;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.BiConsumer;


public class MapDemo {
    public static void main(String[] args) {
        //test1();
        //test2();//遍历第一种方式,遍历key集合(键找值)
        //test3();//遍历第二种方式(键值对)
        test4();//lambda表达式遍历第三种方式(键值对)
    }

    public static void test4() {
        Map<String,Double> map = new TreeMap<>();
        map.put("张三", 88.5);
        map.put("李四", 90.5);
        map.put("王五", 92.5);
        map.put("赵六", 85.5);

        map.forEach(new BiConsumer<String, Double>() {
            @Override
            public void accept(String key, Double value) {
                System.out.println(key + "=" + value);
            }
        });

        System.out.println("--------------------------");
        map.forEach((key, value) -> System.out.println(key + "=" + value));
    }

    public static void test3() {
        Map<String,Double> map = new TreeMap<>();
        map.put("张三", 88.5);
        map.put("李四", 90.5);
        map.put("王五", 92.5);
        map.put("赵六", 85.5);
        //map转化为set集合(map中的key-value绑定城一个对象entry)
        Set<Map.Entry<String, Double>> entrySet = map.entrySet();
        //遍历集合
        for (Map.Entry<String, Double> entry : map.entrySet()) {
            String key = entry.getKey();
            Double value = entry.getValue();
            System.out.println(key + "=" + value);
        }
    }

    public static void test2() {
        Map<String,Double> map = new TreeMap<>();
        map.put("张三", 88.5);
        map.put("李四", 90.5);
        map.put("王五", 92.5);
        map.put("赵六", 85.5);
        //取键放入keySet
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            //根据key取值
            Double value = map.get(key);
            System.out.println(key + ":" + value);
        }
    }

    public static void test1() {
        Map<String,Double> map = new TreeMap<>();
        map.put("张三", 88.5);
        map.put("李四", 90.5);
        map.put("王五", 92.5);
        map.put("赵六", 85.5);
        System.out.println(map);

        map.remove("张三");
        System.out.println(map);

        boolean b1 = map.containsKey("李四");
        System.out.println("b1="+b1);
        boolean b2 = map.containsValue(85.5);
        System.out.println("b2="+b2);

        boolean empty = map.isEmpty();
        System.out.println("empty="+empty);

        int size = map.size();
        System.out.println("size="+size);
    }
}
